package com.xunyin.netty;

/**
 * @author jthong
 * @Description 缓存对象
 * @Version 1.0.0
 * @Date 2019/12/26 16:18
 */
public class Cache {
    private static final Integer TTL_NULL = 60 * 1000;
    private static final Integer TTL_SIMPLE = 24 * 60 * TTL_NULL;

    private Integer ttl;
    private Object value;
    private Long startTime;

    public Integer getTtl() {
        return ttl;
    }

    public Object getValue() {
        if (this.isTimeout()) {
            this.value = null;
        }
        return this.value;
    }

    public Long getStartTime() {
        return startTime;
    }

    public Boolean isTimeout() {
        return System.currentTimeMillis() >= startTime + ttl;
    }

    public void updateCache(Object value) {
        this.value = value;
        this.startTime = System.currentTimeMillis();
        this.ttl = value == null ? TTL_NULL : TTL_SIMPLE;
    }

    public static Cache valueOf(Object value) {
        Cache cache = new Cache();
        cache.updateCache(value);
        return cache;
    }
}